package com.shensi.adminCollect.controller.station;

import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.redis.service.RedisCache;
import com.shensi.adminCollect.domain.station.Enterprise;
import com.shensi.adminCollect.domain.station.Grade;
import com.shensi.adminCollect.domain.station.img;
import com.shensi.adminCollect.service.station.IEnterpriseService;
import com.shensi.adminCollect.service.station.IGradeService;
import com.shensi.adminCollect.service.station.IStationService;
import com.shensi.adminCollect.service.system.ISysUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.*;

import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;
@Api(value = "油企信息管理页面接口",description = "油企信息管理页面接口")
@RestController
@RequestMapping("/syncEnterprise")
public class EnterPriseSyncController extends BaseController {

    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IGradeService gradeService;
    @Autowired
    private IStationService stationService;

    @Autowired
    private RedisCache redisCache;

    @PostMapping("/addEnterprise/{timestamp}/{sign}")
    public AjaxResult add(@RequestBody Enterprise enterprise) throws  Exception
    {

        int i=0;
        String [] codevalue={"1","2","3","4","5"};
        String [] name={"普卡","白银","黄金","钻石","黑卡"};
        Long [] growvalue={0L, 900000000L,900000001L,900000002L,900000003L};
        String regionData1="";
        String licenseImg="";
        String ImgName="";
        if (enterprise.getRegionDataList()!=null&&enterprise.getRegionDataList().length>0) {
            for (String regionData : enterprise.getRegionDataList()) {
                if ("".equals(regionData1)) {
                    regionData1 += regionData;
                } else {
                    regionData1 += "," + regionData;
                }
            }
            if(!"".equals(regionData1)){
                enterprise.setRegionData(regionData1);
            }
        }
        if (enterprise.getImgFileList()!=null&&enterprise.getImgFileList().size()>0) {
            for (img img1 : enterprise.getImgFileList()) {
                if ("".equals(licenseImg)) {
                    licenseImg += img1.getUrl();
                    ImgName +=img1.getName();
                } else {
                    licenseImg +="," +img1.getUrl();
                    ImgName +=","+img1.getName();
                }
            }

            if ("".equals(licenseImg)) {
                enterprise.setLicenseImg(licenseImg);
                enterprise.setLicenseName(ImgName);
            }
        }
        Calendar cal = Calendar.getInstance();
        Random random=new Random();
        String result1 = "";
        for (int m = 0; m < 4; m++) {
            result1 += random.nextInt(10);
        }
        int year = cal .get(Calendar.YEAR);
        int month = cal .get(Calendar.MONTH) + 1;
        int yearcount=year-2000;
        String code="C"+yearcount+month+"0001"+result1;
        enterprise.setEnterpeiseCode(code);
        i= enterpriseService.insertEnterprise(enterprise);
        if(i<=0){
            return toAjax(0);
        }
        Long[] roles={Long.parseLong(18+"")};
        Enterprise enterprise1=enterpriseService.selectEnterpriseList(enterprise).get(0);
        SysUser sysUser=new SysUser();
        sysUser.setDeptId((long) 100);
        sysUser.setUserName(code);
        sysUser.setNickName(enterprise.getEnterpriseName());
        sysUser.setUserLevel("1");
        sysUser.setPhonenumber(enterprise.getPhone());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setEnterpriseId(enterprise1.getEnterpriseId());
        sysUser.setRoleIds(roles);
        i=sysUserService.insertUser(sysUser);
        if(i<=0){
            throw new SQLException("插入用户失败");
        }
        for(int m=0;m<5;m++){
            Grade grade=new Grade();
            grade.setEnterpriseId(enterprise1.getEnterpriseId());
            grade.setGradeGrowValue(growvalue[m]);

            grade.setCreateUser(enterprise.getCreateUser());

            grade.setCreateTime(new Date());
            grade.setGradeName(name[m]);
            grade.setGradeCode(codevalue[m]);
            i= gradeService.insertGrade(grade);
        }
        return AjaxResult.success(enterprise1.getEnterpriseId());
    }


    /**
     * 修改油站信息
     */
    @PostMapping("/updateEnterprise/{timestamp}/{sign}")
    public AjaxResult edit(@RequestBody Enterprise enterprise)
    {
        int i=0;
        String regionData1="";
        String licenseImg="";
        String ImgName="";

        if (enterprise.getRegionDataList()!=null&&enterprise.getRegionDataList().length>0) {
            for (String regionData : enterprise.getRegionDataList()) {
                if ("".equals(regionData1)) {
                    regionData1 += regionData;
                } else {
                    regionData1 += "," + regionData;
                }
            }
            if(!"".equals(regionData1)){
                enterprise.setRegionData(regionData1);
            }
        }
        if (enterprise.getImgFileList()!=null&&enterprise.getImgFileList().size()>0) {
            for (img img1 : enterprise.getImgFileList()) {
                if ("".equals(licenseImg)) {
                    licenseImg += img1.getUrl();
                    ImgName +=img1.getName();
                } else {
                    licenseImg +="," +img1.getUrl();
                    ImgName +=","+img1.getName();
                }
            }

            if ("".equals(licenseImg)) {
                enterprise.setLicenseImg(licenseImg);
                enterprise.setLicenseName(ImgName);
            }
        }
        try {
            redisCache.deleteObject("EnterpriseServiceImpl_selectEnterpriseInfo::" + "e_" +enterprise.getEnterpriseId() );
            i=enterpriseService.updateEnterprise(enterprise);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(i);
    }

    @GetMapping(value = "/{enterpriseId}/{timestamp}/{sign}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") Long enterpriseId)
    {
        Enterprise enterprise=enterpriseService.selectEnterpriseById(enterpriseId);
        if(enterprise.getRegionData()!=null){
            enterprise.setRegionDataList(enterprise.getRegionData().split(","));
        }
        if(enterprise.getLicenseImg()!=null){
            List<img> imgs=new ArrayList<>();
            if(enterprise.getLicenseImg()!=null){
                String[] message= enterprise.getLicenseImg().split(",");
                String[] name= enterprise.getLicenseImg().split(",");

                for(int i=0;i<name.length;i++){
                    img img2=new img();
                    img2.setName(name[i]);
                    img2.setUrl(message[i]);
                    imgs.add(img2);
                }
            }
            enterprise.setImgFileList(imgs);
        }
        return AjaxResult.success(enterprise);
    }



}
